package com.reger.datasource.core;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Map;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

public class TempDataSource implements DataSource {
	
	private static final Logger log = LoggerFactory.getLogger(TempDataSource.class);

	private final Map<String,? extends DataSource> dataSourceMap;
	private DataSource refDataSource;
	private final String dbName;
	private final String refDataSourceName;
	
	private final String useDb;

	public TempDataSource( Map<String,? extends DataSource> dataSourceMap, String refDataSourceName, String dbName) {
		super();
		this.dataSourceMap = dataSourceMap;
		this.refDataSourceName=refDataSourceName;
		this.dbName = dbName;
		this.useDb="use "+dbName+";";
	}
	
	public TempDataSource( DataSource refDataSource, String dbName) {
		super();
		this.refDataSource = refDataSource;
		this.dbName = dbName;
		this.useDb="use "+dbName+";";
		this.refDataSourceName=null;
		this.dataSourceMap=null;
	}
	
	public DataSource getRefDataSource() {
		if(refDataSource!=null) {
			return refDataSource;
		} 
		synchronized (dataSourceMap) {
			if(refDataSource!=null) {
				return refDataSource;
			}
			Assert.isTrue(dataSourceMap.containsKey(refDataSourceName), refDataSourceName+"是不存在的，请检查");
			refDataSource=dataSourceMap.get(refDataSourceName);
			return refDataSource;
		}
	}
	
	@Override
	public PrintWriter getLogWriter() throws SQLException {
		return getRefDataSource().getLogWriter();
	}

	@Override
	public void setLogWriter(PrintWriter out) throws SQLException {
		getRefDataSource().setLogWriter(out);
	}

	@Override
	public void setLoginTimeout(int seconds) throws SQLException {
		getRefDataSource().setLoginTimeout(seconds);
	}

	@Override
	public int getLoginTimeout() throws SQLException {
		return getRefDataSource().getLoginTimeout();
	}

	@Override
	public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException {
		return getRefDataSource().getParentLogger();
	}

	@Override
	public <T> T unwrap(Class<T> iface) throws SQLException {
		return getRefDataSource().unwrap(iface);
	}

	@Override
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		return getRefDataSource().isWrapperFor(iface);
	}

	@Override
	public Connection getConnection() throws SQLException {
		return this.useDb(getRefDataSource().getConnection());
	}

	@Override
	public Connection getConnection(String username, String password) throws SQLException {
		return this.useDb(getRefDataSource().getConnection(username, password));
	}
	
	private Connection useDb(Connection connection) throws SQLException {
		connection.prepareCall(this.useDb).executeQuery();
		log.debug("获取{}的链接",dbName);
		return connection;
	}


	public String getDbName() {
		return dbName;
	}

}
